const { formatDate } = require("../../../utils/util")

// pages/mine/order/order.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {

    active:0,
    list:[],
    pageNum: 1,
    pageSize: 4,
    num:2,
    form:[]
  },

//toPinjia  去评价
toPinjia(e){
  console.log(e);
  let items = e.currentTarget.dataset.items
  let orderCode = items.orderId
  let typeId =items.typeDetailId

  wx.navigateTo({
    url: `/pages/find/releaseNews/releaseNews?type=4&orderCode=${orderCode}&typeId=${typeId}`,
  })

},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let that = this

    // console.log(options)
    that.setData({
      list:[],
      active: +options.id
    })
    console.log("1234",that.data.list)
    
  },

  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
  
    if(this.data.active == 0)
    that.setData({
      num: 0
    })
     
    if(this.data.active==1)
    that.setData({
     
      num: 2
    })
     
    if(this.data.active == 2)
    that.setData({
      num: 3
    })
      
    if(this.data.active == 3)
    that.setData({
      num: 5
    })
     
    if(this.data.active == 4)
    that.setData({
      num: ''
    })
 
    
    var postObj = {
      url: '/mobileUser/sellectMyOrderList?pageNum=' + this.data.pageNum + '&pageSize=' + this.data.pageSize + '&orderStatus='  + this.data.num,
      method: 'get'
    }
    app.request.wxRequest(postObj).then(res => {
      if (res.data.code === 200) {
        // console.log('优惠券', res)
        // console.log(res.data.rows)
        // var list = res.data.rows
        // console.log(res.data.rows[0].data)
        // console.log(JSON.parse(res.data.rows[0].data))
    if(res.data.rows.length >0){
      for(var i = 0;i<res.data.rows.length;i++)
      {
        res.data.rows[i].data = JSON.parse(res.data.rows[i].data)
        // console.log( res.data.rows[i].data)
        var arr = []
        var k =  res.data.rows[i].data
        for( const key in k){
          const element =k[key]
          // console.log(element)
          arr.push(element)
        }
        res.data.rows[i].data = arr
        
      }
    }  
        var navList = this.data.list
        // console.log("123",navList)
        var rows = res.data.rows
        var allArr = [...navList, ...rows]
        this.setData({
          list  : allArr
        })
        if(this.data.num ===3 ){
          var arrlist =[] 
          
          for(var i = 0;i< this.data.list.length;i++){
            if(this.data.list[i].evaluationStatus === 0)
             arrlist = arrlist.push(this.data.list[i]) 
          }
          this.setData({
            list  : arrlist
          })
          // console.log(this.data.list)
        }
        // console.log(this.data.list)
      }
    })


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    that.setData({
      pageNum: that.data.pageNum + 1
    })
    that.onShow()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onChange(event) {
    // console.log(event)
    this.setData({
      active:event.detail.name,
      list:[],
      pageNum:1,
    })
    this.onShow()
  },
  quxiao(e){
    console.log(e)
    this.setData({
      'form.orderId':e.currentTarget.dataset.orderid
    })
      var that = this
      wx.showModal({
        title: '警告',
        content: '确认取消吗',
        success(res) {
          if (res.confirm) {
            var shanchuList = {
              url: '/mobileUser/cancelServiceOrder',
              method: 'post',
              data:that.data.form
            }
            app.request.wxRequest(shanchuList).then(res => {
              if (res.data.code === 200) {
                that.setData({
                  list:[],
                  pageNum:1
                })
                that.onShow()
              }
            });
    
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
    
        }
      })
    }

})